package com.hzy.listview;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.hzy.login.UserDBHelper;
import com.hzy.notepad.NotepadActivity;
import com.hzy.settings.SettingActivity;
import com.hzy.text.R;
import com.sam.widget.headerBar.LeftImgTitleBar;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24 0024.
 */
public class NotepadListActivity extends AppCompatActivity{

    private ListView recordListView;
    private ImageView penBtn;
    private String userName;
    private UserDBHelper userDBHelper;
    private SQLiteDatabase db;
    private LeftImgTitleRightImgBar leftImgTitleRightImgBar;
    private List<NotepadRecordEntity> notesData;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_listview);
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        userDBHelper =new UserDBHelper(this,"User.db",null,1);
        db = userDBHelper.getWritableDatabase();
        findView();
        initView();
    }



    private void findView(){
        recordListView = (ListView) findViewById(R.id.record_list);
        penBtn = (ImageView) findViewById(R.id.pen_btn);
        leftImgTitleRightImgBar = (LeftImgTitleRightImgBar) findViewById(R.id.list_title);
    }

    private void initView(){
        notesData = getRecordData();
        recordListView.setAdapter(new NotepadRecordAdapter(this,notesData,userName));
        penBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(NotepadListActivity.this, NotepadActivity.class);
                intent.putExtra("userName",userName);
                startActivity(intent);
            }
        });

        leftImgTitleRightImgBar.setOnTitleLeftClickListener(new LeftImgTitleBar.OnTitleLeftClickListener() {
            @Override
            public void onLeftClick() {
                Intent it = new Intent(NotepadListActivity.this, SettingActivity.class);
                startActivity(it);
            }
        });

        recordListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NotepadListActivity.this,NotepadActivity.class);
                intent.putExtra("note",notesData.get(position).getNoteText());
                intent.putExtra("noteTitle",notesData.get(position).getNoteTitle());
                intent.putExtra("id",notesData.get(position).get_id());
                intent.putExtra("userName",userName);
                startActivity(intent);
            }
        });
    }

    private List<NotepadRecordEntity> getRecordData(){
        List<NotepadRecordEntity> list = new ArrayList<>();


        Cursor cursor = db.rawQuery("select * from Note where userName = ?",new String[]{userName});
        if (cursor.moveToFirst()){
              do {
                  NotepadRecordEntity entity = new NotepadRecordEntity();
                  entity.setNoteText(cursor.getString(cursor.getColumnIndex("note")));
                  entity.setNoteTitle(cursor.getString(cursor.getColumnIndex("noteTitle")));
                  entity.setDateTime(cursor.getString(cursor.getColumnIndex("dateTime")));
                  entity.set_id(cursor.getString(cursor.getColumnIndex("_id")));
                list.add(entity);
            } while (cursor.moveToNext());
        }
        return list;
    }
}
